package com.dong.com.love.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;

import com.dong.com.love.adapter.NewsAdapter;
import com.dong.com.love.cach.CachCallBack;
import com.dong.com.love.listener.CardItemClickListener;
import com.dong.com.love.model.HeadModel;
import com.dong.com.love.Bean.NewsPic;
import com.dong.com.love.model.InitMoudel;
import com.dong.com.mvvmdemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dong
 */
public class MainActivity extends AppCompatActivity implements CardItemClickListener {

    private FloatingActionButton fab;
    private Toolbar toolbar;
    private ListView lv;
    private CollapsingToolbarLayout collapsingToolbar;
    private List<NewsPic.TngouBean> list;
    private NewsPic newsPic;
    private NewsAdapter adapter;
    private AppBarLayout appBarLayout;
    private Context context;
    private String cachName="news";
    private int cachTime=24*60*60*1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context=this;

        initView();

        initData();
    }

    /*
      初始化View
     */
    private void initView() {
        //初始化标题栏
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        appBarLayout = (AppBarLayout) findViewById(R.id.main_appbar);
        collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setExpandedTitleColor(getResources().getColor(R.color.colorAccent));
        collapsingToolbar.setCollapsedTitleTextColor(Color.WHITE);
        //初始化ListView
        lv = (ListView) findViewById(R.id.main_lv);
        //初始化悬浮按钮
        fab =  (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appBarLayout.setExpanded(false);
            }
        });
    }

    /*
      初始化数据
     */
    private void initData() {
        list = new ArrayList<NewsPic.TngouBean>();
        initAdapter();
        updateData();
    }

    /*
      更新数据
     */
    private void updateData() {

        new InitMoudel<NewsPic>(context, NewsPic.class, cachName, cachTime) {

            @Override
            public void getDataFromNet() {
                HeadModel.getNews(context, new CachCallBack<NewsPic>(context,cachName) {
                    @Override
                    public void update(NewsPic newsPic) {
                        bindData(newsPic);
                    }
                });
            }

            @Override
            public void doResult(NewsPic newsPic) {
                bindData(newsPic);
            }
        }.start();

    }

    /*
      绑定数据
     */
    private void bindData(NewsPic newsPic){
        List<NewsPic.TngouBean> tngou = newsPic.getTngou();
        if(tngou!=null){
            list.clear();
            list.addAll(tngou);
            initAdapter();
        }
    }

    /*
      适配数据
     */
    private void initAdapter() {
        if (adapter == null) {
            adapter = new NewsAdapter(this,list);
            lv.setAdapter(adapter);
            adapter.setCardItemClickListener(this);
        }else{
            adapter.notifyDataSetChanged();
        }
    }

    /*
      Item点击响应
     */
    @Override
    public void onCardClickListener(int positon) {
        Intent intent = new Intent(this, ImageShowActivity.class);
        NewsPic.TngouBean tngouBean = list.get(positon);
        int id = tngouBean.getId();
        intent.putExtra("id", id);
        startActivity(intent);
    }
}
